package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.PatientCategoryMapper;
import com.itheima.pojo.Member;
import com.itheima.pojo.PatientCategory;
import com.itheima.service.PatientCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = PatientCategoryService.class)
@Transactional
public class PatientCategoryServiceImpl implements PatientCategoryService {

   @Autowired
   private PatientCategoryMapper patientCategoryMapper;
   @Autowired
   private MemberMapper memberMapper;

   public PageResult findPage(QueryPageBean queryPageBean) {
      //1.获取参数
      Integer currentPage = queryPageBean.getCurrentPage();
      Integer pageSize = queryPageBean.getPageSize();
      String queryString = queryPageBean.getQueryString();
      //2.使用分页助手
      PageHelper.startPage(currentPage, pageSize);
      //3.调用条件查询
      Page<PatientCategory> page = memberMapper.findByCondition2(queryString);
      return new PageResult(page.getTotal(),page.getResult());
   }

   //根据id查询会员详情,包括会员体检信息,包括评估信息
   public Member findById(Integer id) {
      Member member = memberMapper.findDetailById(id);
      return member;
   }

   //查询所有分组名称
   public String[] findAll() {
      String[] categorys = patientCategoryMapper.findAll();
      return categorys;
   }

   //通过会员id查询分组名称
   public String[] findCategoryNamesByMemberId(Integer memberId) {
      String[] categoryNames = patientCategoryMapper.findCategoryNamesByMemberId(memberId);
      return categoryNames;
   }

   //确认人群分类
   public void setCategory(Map map) {
      //0.获取参数
      Integer memberId = (Integer) map.get("memberId");//会员id
      List<String> checkList = (List<String>) map.get("checkList");//分类列表
      Integer healthManagerId = (Integer) map.get("healthManagerId");//健康管理师id

      //1.修改会员表的分类状态为 "人为分类"
      memberMapper.editCategoryStatus(memberId);
      //2.修改会员对应的分类
      //2.1删除会员和分类的关系
      patientCategoryMapper.deleteCategoryByMemberId(memberId);
      //2.2插入新的关系
      this.batchInsertCategory(memberId,checkList);
      //3.修改会员对应的健康管理师
      memberMapper.editHealthManager(memberId,healthManagerId);

   }

   private void batchInsertCategory(Integer memberId, List<String> checkList) {
      //遍历集合,获取分组id
      List<Map> list = new ArrayList<>();
      Map map = null;
      for (String categoryName : checkList) {
         Integer categoryId = patientCategoryMapper.findIdByCategoryName(categoryName);
         map = new HashMap();
         map.put("categoryId",categoryId);
         map.put("memberId", memberId);

         list.add(map);
      }

      patientCategoryMapper.batchInsert(list);
   }
}
